﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DiscMaster.Web.Models;
using System.Web.UI.DataVisualization.Charting;

namespace DiscMaster.Web.Extensions
{
    public static class Graph
    {
        public static List<System.Web.UI.DataVisualization.Charting.Series> ToChartSeries(this DiscMaster.Web.Models.Match result, System.Web.UI.DataVisualization.Charting.SeriesChartType valuetype, System.Web.UI.DataVisualization.Charting.SeriesChartType indextype)
        {
            System.Web.UI.DataVisualization.Charting.Series ser;
            List<System.Web.UI.DataVisualization.Charting.Series> ret = new List<System.Web.UI.DataVisualization.Charting.Series>();

            ser = new System.Web.UI.DataVisualization.Charting.Series("par");
            ser.MarkerSize = 5;
            ser.MarkerStyle = MarkerStyle.Diamond;
            ser.ChartType = indextype;
            foreach (Hole hole in result.CourseLayout.Holes.OrderBy(h => h.number))
            {
                ser.Points.AddXY(hole.number, hole.par);
            }
            ret.Add(ser);

            foreach (var player in result.MatchResults.GroupBy(p => p.playerid))
            {
                ser = new System.Web.UI.DataVisualization.Charting.Series(player.First().Player.UserName);
                ser.MarkerSize = 7;
                ser.MarkerStyle = MarkerStyle.Circle;
                ser.ChartType = valuetype;
                foreach (MatchResult res in player.OrderBy(r => r.Hole.number))
                {
                    ser.Points.AddXY(res.Hole.number, res.result);
                }
                ret.Add(ser);
            }
            return ret;
        }
    }
}
